<template>
    <rx-layout style="padding: 10px;">
        <template #middle>
            <rx-fit v-model="fitSearch">
                <template #head>
                  <div style="display: flex;align-items: center;justify-content: space-between;">
                    <div class="table-operator">
                      <a-button type="primary" @click="add">
                        <template #icon>
                          <plus-outlined/>
                        </template>
                        {{$ti18('rxList.newBtn','新建')}}
                      </a-button>
                      <a-button v-if="removeEnable && selectedRowKeys.length > 0" type="danger" @click="delByIds(selectedRowKeys)">
                        <template #icon>
                          <DeleteOutlined />
                        </template>
                        {{$ti18('rxList.delBtn','删除')}}
                      </a-button>
                    </div>
                    <span class="search-btn" @click.stop="searchshow" @mousedown.stop><SearchOutlined/>{{ $ti18('rxList.leachBtn', '过滤') }}</span>
                  </div>
                </template>
                <template #search>
                  <a-form :layout="colLayout">
                    <a-row :gutter="24">
                      <a-col :span="colSpan">
                        <a-form-item :label="$ti18('rxList.title', '标题')">
                          <a-input :placeholder="$ti18('rxList.title', '标题','','input')" v-model:value="urlConfig.queryParam.Q_MSG_TITLE__S_LK"/>
                        </a-form-item>
                      </a-col>
                      <a-col :span="colSpan">
                        <a-form-item :label="i18n('timeFrom','消息创建时间 从')">
                          <a-date-picker
                              style="width: 100%"
                              :placeholder="i18n('createTime','请输入任务创建时间')"
                              v-model:value="urlConfig.queryParam['Q_CREATE_TIME__D_GT']"
                              format="YYYY-MM-DD"
                          />
                        </a-form-item>
                      </a-col>
                      <a-col :span="colSpan">
                        <a-form-item :label="i18n('to','至')">
                          <a-date-picker
                              style="width: 100%"
                              :placeholder="i18n('createTime','请输入任务创建时间')"
                              v-model:value="urlConfig.queryParam['Q_CREATE_TIME__D_LT']"
                              format="YYYY-MM-DD"
                          />
                        </a-form-item>
                      </a-col>
                    </a-row>
                  </a-form>
                  <div class="list-search-toolbar">
                    <a-button type="primary" @click="search">
                      <SearchOutlined/>
                      {{ $ti18('rxList.searchBtn', '查询') }}
                    </a-button>
                    <a-button style="margin-left: 4px" @click="resetSearch">
                      <RedoOutlined />
                      {{ $ti18('rxList.resetBtn', '重置') }}
                    </a-button>
                  </div>
                </template>
                <template #middle>
                  <rx-grid ref="table"
                           :hasPage="true"
                           :urlConfig="urlConfig"
                           :selectConfig="selectConfig"
                           url="/api-portal/portal/core/infInnerMsg/querySentMsg"
                           field="msgId"
                           :columns="columns"
                  >
                    <template #userList="{row}">
                      <a-tag v-for="user in row.userList"><UserOutlined />{{ user.name }}</a-tag>
                    </template>
                    <template #groupList="{row}">
                      <a-tag v-for="group in row.groupList"><TeamOutlined />{{ group.name }}</a-tag>
                    </template>
                    <template #appId="{row}">
                      {{allApps[row.appId]?$i18(allApps[row.appId].appName,'应用名称'):$ti18('rxList.noAppName','无')}}
                    </template>
                    <template #action="{ row }">
                      <RxGridAction :size="2">
                        <div @click.stop="readMessage(row)">{{i18n('list.view','查看')}}</div>
                        <div v-if="removeEnable" @click.stop="delByIds([row.msgId])">{{$ti18('rxList.delBtn','删除')}}</div>
                      </RxGridAction>
                    </template>
                  </rx-grid>
                  <div>
                    <a-drawer
                        :title="i18n('list.viewMsg','查看消息')"
                        placement="right"
                        :closable="false"
                        :open="visible"
                        @close="onClose"
                        :width="400"
                    >
                      <ReadMessage :infInnerMsg="infInnerMsg" :isSender="true"></ReadMessage>
                    </a-drawer>
                  </div>
                </template>
            </rx-fit>
        </template>
    </rx-layout>
</template>

<script>
    import InfInboxApi from '@/common/api/portal/infInbox'
    import InfInnerMsgEdit from './InfInnerMsgEdit.vue'
    import ReadMessage from './ReadMessage.vue'
    import {BaseList, Util} from '@lowcode/jpaas-base-lib'
    import { mapState} from 'pinia';
    import useMain from '@/stores/useMain';

    export default {
        name: 'InfInboxList',
        mixins: [BaseList],
        components: {
            ReadMessage,
        },
        computed: {
          ...mapState(useMain,['menus']),
          allApps(){
            let json = {};
            this.menus.forEach(item=>{
              json[item.id]={appName:item.title};
            });
            return json;
          }
        },
        data() {
            return {
                urlConfig: {
                  ajaxType: 'post',
                  contentType: 'json',
                  queryParam: {},
                  dataField: 'result.data',
                  totalField: 'result.totalCount'
                },
                selectConfig: {
                  type: 'checkbox',
                },
                columns: [
                    {title: this.$ti18('rxList.title','标题'), field: 'msgTitle', minWidth: 180},
                    {title: this.i18n('form.addressee','收信人'), field: 'userList', minWidth: 140, slots: {default: 'userList'}},
                    {title: this.i18n('form.reSection','收信组'), field: 'groupList', minWidth: 140, slots: {default: 'groupList'}},
                    {
                        title: this.i18n('form.app','所属应用'),
                        field: 'appId',
                        minWidth: 140,
                        slots: {default: 'appId'}
                    },
                    {title: this.i18n('form.sendTime','发送时间'), field: 'createTime', minWidth: 120},
                    {title: this.$ti18('rxList.action','操作'), minWidth: 80, field: 'action', slots: {default: 'action'}},
                ],
                component: InfInnerMsgEdit,
                comment: this.i18n('list.shortMsg','内部短消息'),
                widthHeight: ['800px', '600px'],
                visible: false,
                infInnerMsg: {},
            }
        },
        methods: {
            i18n(name, text, key) {
                return this.$ti18(name, text, "infInbox", key);
            },
            delByIds(msgIds) {
                InfInboxApi.delSentMsg({msgIds: msgIds.join(',')}).then((res) => {
                    this.$refs.table.loadData()
                })
                return
            },
            readMessage(record) {
                this.visible = true
                this.infInnerMsg = record
            },
            onClose() {
                this.visible = false
            },
        },
    }
</script>
<style scoped>

</style>

